﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Repositories;
using DomenModel;
using System.Data.Entity;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;

namespace Infrastructure.Repositories
{
    public class RoleRepo : ICrud<Role>
    {

        /// <summary>
        /// Gets the Role by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
          public Role GetById(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                Role roleById = _db.Roles.FirstOrDefault(p => p.RoleId == Id);
                return roleById;
            }

        }

          /// <summary>
          /// Gets the Role by name.
          /// </summary>
          /// <param name="Name">The name.</param>
          /// <returns></returns>
          public Role GetByName(string Name)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                Role roleByName = _db.Roles.FirstOrDefault(p => p.Name == Name);
                return roleByName;
            }

        }

          /// <summary>
          /// Saves the Role to database.
          /// </summary>
          /// <param name="role">The role.</param>
          public void Save(Role role)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  _db.Roles.Add(new Role
                  {
                      Name = role.Name,
                      Description = role.Description,

                  });
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Updates the Role.
          /// </summary>
          /// <param name="roleUpdate">The role update.</param>
          public void Update(Role roleUpdate)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  Role roleById = _db.Roles.First(p => p.RoleId == roleUpdate.RoleId);
                  roleById.Name = roleUpdate.Name;
                  roleById.Description = roleUpdate.Description;
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Deletes the Role from database.
          /// </summary>
          /// <param name="Id">The id.</param>
          public void Delete(long Id)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  Role roleById = _db.Roles.First(p => p.RoleId == Id);
                  _db.Roles.Remove(roleById);
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Gets the collection of Role.
          /// </summary>
          /// <returns></returns>
          public List<Role> GetCollection()
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var listOfRoles = _db.Roles.ToList();
                return listOfRoles;
            }
        }

        
    
    }
}
